package com.kmmlearn.findtime.android.ui.components

import androidx.compose.animation.core.Spring
import androidx.compose.animation.core.exponentialDecay
import androidx.compose.animation.core.spring
import androidx.compose.animation.core.tween
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.background
import androidx.compose.foundation.gestures.*
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.IntOffset
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.compose.ui.platform.LocalDensity
import kotlin.math.roundToInt

// 定义状态枚举（必须有，否则无法推断泛型类型）
private enum class CardState { Collapsed, Expanded }

@OptIn(ExperimentalFoundationApi::class)
@Composable
fun MultiActionAnchoredCard(
    title: String,
    subtitle: String,
    onCollect: () -> Unit = {},
    onEdit: () -> Unit = {},
    onDelete: () -> Unit = {}
) {
    val buttonWidth = 80.dp
    val totalButtons = 3
    val totalButtonWidthPx = with(LocalDensity.current) { (buttonWidth * totalButtons).toPx() }

    // 定义锚点
    val anchors = DraggableAnchors<CardState> {
        CardState.Collapsed at 0f
        CardState.Expanded at -totalButtonWidthPx
    }

    // 明确指定泛型类型为 CardState
    val draggableState = remember {
        AnchoredDraggableState<CardState>(
            initialValue = CardState.Collapsed,
            positionalThreshold = { distance: Float -> distance * 0.3f },
            velocityThreshold = { 100f },
            snapAnimationSpec = spring(Spring.DampingRatioMediumBouncy),
            decayAnimationSpec = exponentialDecay()
        ).apply {
            updateAnchors(anchors)
        }
    }



    Box(
        modifier = Modifier
            .fillMaxWidth()
            .height(60.dp)
    ) {
        // 背景按钮
        Row(
            modifier = Modifier
                .fillMaxSize()
                .align(Alignment.CenterEnd),
            horizontalArrangement = Arrangement.End
        ) {
            ActionButton("收藏", Color(0xFF1976D2), onCollect)
            ActionButton("编辑", Color(0xFFFF9800), onEdit)
            ActionButton("删除", Color(0xFFF44336), onDelete)
        }

        // 前景卡片（滑动）
        Card(
            modifier = Modifier
                // 将锚点偏移量应用到组件上
                .offset { IntOffset(draggableState.requireOffset().roundToInt(), 0) }
                .anchoredDraggable(
                    state = draggableState,
                    orientation = Orientation.Horizontal
                )
                .fillMaxSize(),
            shape = RoundedCornerShape(0.dp),
            colors = CardDefaults.cardColors(containerColor = Color.White)
        ) {
            Row(
                modifier = Modifier
                    .fillMaxSize()
                    .padding(horizontal = 16.dp),
                verticalAlignment = Alignment.CenterVertically,
                horizontalArrangement = Arrangement.SpaceBetween
            ) {
                Text(text = title, fontSize = 16.sp)
                Text(text = subtitle, color = Color.Gray, fontSize = 14.sp)
            }
        }
    }
}

@Composable
fun ActionButton(text: String, color: Color, onClick: () -> Unit) {
    Box(
        modifier = Modifier
            .width(80.dp)
            .fillMaxHeight()
            .background(color),
        contentAlignment = Alignment.Center
    ) {
        Text(text = text, color = Color.White, textAlign = TextAlign.Center)
    }
}

